package com.abc.scholarship.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.abc.scholarship.common.constants.CacheConstant;
import com.abc.scholarship.common.utils.UserHolder;
import com.abc.scholarship.domain.dto.NoticeDTO;
import com.abc.scholarship.domain.entity.Notice;
import com.abc.scholarship.domain.vo.resp.NoticeResp;
import com.abc.scholarship.exception.BusinessException;
import com.abc.scholarship.mapper.NoticeMapper;
import com.abc.scholarship.service.NoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-07-08  11:18
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private NoticeMapper noticeMapper;


    @Override
    public List<NoticeResp> getList(Integer year) {
        // 查缓存
        String key = CacheConstant.getKey(CacheConstant.NOTICE_YEAR, year);
        String resStr = stringRedisTemplate.opsForValue().get(key);
        if (Objects.nonNull(resStr)) {
            return JSONUtil.toList(resStr, NoticeResp.class);
        }
        // 查找数据库
        List<NoticeResp> collect = list(new LambdaQueryWrapper<Notice>()
                .eq(Notice::getYear, year))
                .stream()
                .map(item -> BeanUtil.copyProperties(item, NoticeResp.class))
                .collect(Collectors.toList());
        setRedis(key, collect);
        return collect;
    }

    private void setRedis(String key, List<NoticeResp> collect) {
        stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(collect),
                CacheConstant.APPLY_RES_EXPIRE_TIME, TimeUnit.HOURS);
    }

    @Override
    public void insert(NoticeDTO noticeDTO) {
        if (noticeDTO.getTitle() == null || noticeDTO.getTitle().isEmpty()) {
            throw new BusinessException("不能输入为空");
        }
        if (noticeDTO.getContent() == null || noticeDTO.getContent().isEmpty()) {
            throw new BusinessException("不能输入为空");
        }
        Long userId = UserHolder.getUser().getId();
        String userName = UserHolder.getUser().getUName();
        Notice notice = new Notice().builder()
                .uName(userName)
                .uId(userId)
                .content(noticeDTO.getContent())
                .title(noticeDTO.getTitle())
                .year(LocalDate.now().getYear())
                .build();
        noticeMapper.insert(notice);
        stringRedisTemplate.delete(CacheConstant.getKey(CacheConstant.NOTICE_YEAR, LocalDate.now().getYear()));
    }
}
